# -*- coding: utf-8 -*-

"""
运行用例集：
    python3 run.py
"""
import pytest
import shutil
import os
from common import log
from common import shell

project_name = 'dz_service'
case_data = 'case_data'
if __name__ == '__main__':
    import project

    project.project_name = project_name
    project.case_data = case_data
    from config import config

    conf = config.Config()
    log = log.logger
    log.info('初始化配置文件, path=config/config.json', extra={'appname': '网厅用例开始执行'})

    shell = shell.Shell()
    xml_report_path = conf.xml_report_path
    html_report_path = conf.html_report_path
    report_path = conf.report_path
    report_path_backup = conf.report_path_backup
    project_path = conf.project_path
    mark = conf.mark

    # 定义测试集
    # args = ['dz_service/test_case/test_01_update_phone.py', '-s', '-q', '--alluredir', xml_report_path]
    # args = ['dz_service/test_case/test_02_reset_pwd.py', '-s', '-q', '--alluredir', xml_report_path]
    # args = ['dz_service/test_case/test_03_permission_open.py', '-s', '-q', '--alluredir', xml_report_path]
    # args = ['dz_service/test_case/test_04_update_id_card.py', '-s', '-q', '--alluredir', xml_report_path]
    # args = ['-m', "permission or id_card", '-s', '-q', '--alluredir', xml_report_path]
    args = [project_path, '-m', mark, '-s', '-q', '--alluredir', xml_report_path]

    # 清空历史报告
    cleanCmd = 'rd /s/q %s' % (report_path)
    shell.invoke(cleanCmd)
    if not os.path.exists(xml_report_path):
        # 如果目标路径不存在原文件夹的话就创建
        os.makedirs(xml_report_path)
    if not os.path.exists(html_report_path):
        # 如果目标路径不存在原文件夹的话就创建
        os.makedirs(html_report_path)

    pytest.main(args)
    cmd = 'allure generate %s -o %s --clean' % (xml_report_path, html_report_path)

    try:
        shell.invoke(cmd)
        shutil.copytree(report_path, report_path_backup)
        print('allurer日志备份成功。')
    except Exception:
        log.error('执行用例失败，请检查环境配置')
        raise
